//获取应用实例
var app = getApp()
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page:0,
    searchVal: ''
  }, 
  onLoad: function (e) {
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    that.skillGoodList();
  },
  skillGoodList:function(){
    var that = this;
    var page=that.data.page;
    var searchVal = that.data.searchVal;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_mall_limit_goods',
        name: searchVal,
        page:page
      },
      success: function (res) {
        console.log(res.data.data)
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.shopGoods;
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            shopGoods: allArr
          })
          if (lastPageLength <=10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.shopGoods);
        } else {
          console.log(res.data)
          if (page <= 0) {
            that.setData({
              shopGoods: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    // var lng = that.data.lng;
    // var lat = that.data.lat;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      searchVal:''
    });
    that.skillGoodList();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    // var lng = that.data.lng;
    // var lat = that.data.lat;
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.skillGoodList();
    }
  },
  searchValChange: function (e) {
    var searchVal = e.detail.value;
    this.setData({
      searchVal: searchVal
    });
  },
  searchGood: function () {
    var that = this;
    var searchVal = that.data.searchVal;
    if (searchVal.length > 0) {
      console.log(searchVal);
      that.skillGoodList();
    } else {
      app.errorTip(that, '请输入搜索商品关键字', 2000);
    }

  },
  cancel: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  openGoodDetail:function(e){
    var that = this;
    var goodid = e.currentTarget.dataset.id;
    var laid = e.currentTarget.dataset.laid;
    wx.navigateTo({
      url: '/subpages1/goodDetail/goodDetail?id=' + goodid+'&laid='+laid,
    })
  },
  onShareAppMessage: function () {
    var that = this;
    var title = '商品秒杀';
    var shareInfo = app.globalData.shareInfo;
    title = shareInfo.shareTitle ? shareInfo.shareTitle : title;
    var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/subpages1/searchSeckill/searchSeckill?enterfrom=share'
    }
  }
})
